package com.ivanceras.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.FocusEvent;
import com.google.gwt.event.dom.client.FocusHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RichTextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.ivanceras.client.Multiplechoice;
import com.ivanceras.client.SimulationService;
import com.ivanceras.client.SimulationServiceAsync;
import com.ivanceras.client.Questionnaire;

public class QuestionnaireEdit{
	private final SimulationServiceAsync simulationService = GWT
	.create(SimulationService.class);

	final VerticalPanel questionnaireEditPanel = new VerticalPanel();
	Questionnaire questionnaire;
	final TextBox titleBox = new TextBox();
	final RichTextArea descriptionBox = new RichTextArea();
	final String titleBoxCaption = "Title...";  
	final String descriptionCaption = "Description....";
	public Multiplechoice multipleChoice;
	
	public long key; //questionnaire key
	public QuestionnaireEdit(){
		titleBox.setText(titleBoxCaption);
		titleBox.setStyleName("new-questionnaire-title");
		descriptionBox.setHTML(descriptionCaption);
	}
	public QuestionnaireEdit(Questionnaire qnr){
		questionnaire = qnr;
		titleBox.setText(qnr.title);
		descriptionBox.setHTML(qnr.description);
	}
	public Widget getWidget(){
		return getEditorWidget();
	}
	public Widget getEditorWidget(){
		final DecoratedPopupPanel qnrTitleTip = new DecoratedPopupPanel(false);
	    qnrTitleTip.add(new HTML("Type the title of the questionnaire"));
		titleBox.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
		       final Widget source = (Widget) event.getSource();
	            //put a timer when to show
	            final Timer t = new Timer() {
			      public void run() {
			            qnrTitleTip.show();
			            int left = source.getAbsoluteLeft() + source.getOffsetWidth();
			            int top = source.getAbsoluteTop() - qnrTitleTip.getOffsetHeight();
			            qnrTitleTip.setPopupPosition(left, top);
			      }
			    };
			    t.schedule(300);//in ms
	            final Timer t2 = new Timer() {
				      public void run() {
				            qnrTitleTip.hide();
				      }
				};
				t2.schedule(3000); //hide after 3 seconds    
			}
			
		});
		titleBox.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				qnrTitleTip.hide();
			}
			
		});
		titleBox.addFocusHandler(new FocusHandler(){

			@Override
			public void onFocus(FocusEvent event) {
				if(titleBoxCaption.equals(titleBox.getText())){
					titleBox.setText("");
				}
				titleBox.setStyleName("new-questionnaire-title-focused");
			}
			
		});
		titleBox.setFocus(true);
		descriptionBox.setSize("350px", "80px");
		final DecoratedPopupPanel qnrDescTooltip = new DecoratedPopupPanel(false);
		qnrDescTooltip.add(new HTML("Type questionnaire description..."));
		descriptionBox.addFocusHandler(new FocusHandler(){

			@Override
			public void onFocus(FocusEvent event) {
				if(descriptionBox.getText().equals(descriptionCaption)){
					descriptionBox.setHTML("");
				}
			}
			
		});
		descriptionBox.addMouseOverHandler(new MouseOverHandler(){

			@Override
			public void onMouseOver(MouseOverEvent event) {
		       final Widget source = (Widget) event.getSource();
	            //put a timer when to show
	            final Timer t = new Timer() {
			      public void run() {
			    	    qnrDescTooltip.show();
			            int left = source.getAbsoluteLeft() + source.getOffsetWidth();
			            int top = source.getAbsoluteTop() - qnrDescTooltip.getOffsetHeight();
			            qnrDescTooltip.setPopupPosition(left, top);
			      }
			    };
			    t.schedule(300);//in ms
	            final Timer t2 = new Timer() {
				      public void run() {
				    	  qnrDescTooltip.hide();
				      }
				};
				t2.schedule(3000); //hide after 3 seconds    
			}
			
		});
		descriptionBox.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				qnrDescTooltip.hide();
			}
			
		});
		Button saveButton = new Button("Save");

		HorizontalPanel qnaireDetailPanel = new HorizontalPanel();
		VerticalPanel qnaireDetail = new VerticalPanel();
		final Image qrImg = new Image();
		qrImg.setUrl("http://profile.ak.fbcdn.net/object2/1066/101/n252547355808_9534.jpg");
		qrImg.setStyleName("questionnaire-image");
		final DecoratedPopupPanel uploadTooltip = new DecoratedPopupPanel(false);
	    uploadTooltip.add(new HTML("Upload image for this questionnaire"));
		qrImg.addMouseOverHandler(new MouseOverHandler(){
			@Override
			public void onMouseOver(final MouseOverEvent event) {
		       final Widget source = (Widget) event.getSource();
	            //put a timer when to show
	            final Timer t = new Timer() {
			      public void run() {
			            uploadTooltip.show();
			            int left = source.getAbsoluteLeft() + source.getOffsetWidth();
			            int top = source.getAbsoluteTop() - uploadTooltip.getOffsetHeight();
			            uploadTooltip.setPopupPosition(left, top);
			      }
			    };
			    t.schedule(200);//in ms
			    t.schedule(300);//in ms
	            final Timer t2 = new Timer() {
				      public void run() {
				    	  uploadTooltip.hide();
				      }
				};
				t2.schedule(3000); //hide after 3 seconds    
			}
		});
		qrImg.addMouseOutHandler(new MouseOutHandler(){

			@Override
			public void onMouseOut(MouseOutEvent event) {
				uploadTooltip.hide();
			}
			
		});
		qnaireDetailPanel.add(qrImg);
		HTML hspacer = new HTML();
		hspacer.setWidth("10px");
		qnaireDetailPanel.add(hspacer);
		qnaireDetail.add(titleBox);
		HTML vspacer = new HTML();
		vspacer.setHeight("5px");
		qnaireDetail.add(vspacer);
		qnaireDetail.add(descriptionBox);
		qnaireDetailPanel.add(qnaireDetail);
	    
	    questionnaireEditPanel.add(qnaireDetailPanel);
	    
		questionnaireEditPanel.add(saveButton);
		saveButton.setWidth("100px");
		saveButton.addClickHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				if(questionnaire != null){//save questionnaire if questionnaire is supplied
					saveQuestionnaire(questionnaire.questionnaireID, titleBox.getText(), descriptionBox.getText());
				}
				else{
					createQuestionnaire(titleBox.getText(), descriptionBox.getText());
				}
			}
		});
		return questionnaireEditPanel;
	}
	
	public void createQuestionnaire(String title, String description){
		simulationService.createQuestionnaire(title, description, new AsyncCallback<Questionnaire>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error creating questionnaire!");
			}

			@Override
			public void onSuccess(Questionnaire qnaire) {
				//Window.alert("Questionnaire sucessfully created!");
				//questionnaireListPanel.setVisible(false);
				//center.add(qnaire.getWidget());
				multipleChoice.addQuestionnaireDisplay(qnaire);
				multipleChoice.restoreTeaserBox();//back to the Box rather than the editor
			}
			
		});
	}
	public void saveQuestionnaire(long questionnaireKey, String title, String description){
		simulationService.saveQuestionnaire(questionnaireKey, title, description, new AsyncCallback<Questionnaire>(){

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Error creating questionnaire!");
			}

			@Override
			public void onSuccess(Questionnaire qnaire) {
				Window.alert("Questionnaire saved!");
				//questionnaireListPanel.setVisible(false);
				//center.add(qnaire.getWidget());
				//multipleChoice.addQuestionnaireDisplay(qnaire);
			}
			
		});
	}

}
